/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.util.ArrayList;
import java.util.List;
import org.cx1.dao.api.IBlogDao;
import org.cx1.model.Blog;

/**
 *
 * @author ECH
 */
public class BlogDao implements IBlogDao {

    private SqlMapClient sqlMapClient;

    public BlogDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
    
    @Override
    public List<Blog> getAll() {
        List<Blog> amenities = new ArrayList<Blog>();
        
        try {
            amenities = sqlMapClient.queryForList("blog.getAll");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return amenities;
    }

    @Override
    public Blog getById(String id) {
        Blog blog = null;
        
        try {
            blog = (Blog) sqlMapClient.queryForObject("blog.getById", id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return blog;
    }

    @Override
    public String add(Blog blog) {
        String blogId = "";
        
        try {
            sqlMapClient.insert("blog.add", blog);
            blogId = blog.getId();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return blogId;
    }

    @Override
    public int update(Blog blog) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("blog.update", blog);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return noOfRowsAffected;
    }

    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("blog.deleteById", id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return noOfRowsAffected;
    }
    
}
